package com.bjsxt.sm.spring.mvc.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONObject;
import com.bjsxt.sm.common.controller.BaseController;
import com.bjsxt.sm.common.util.ConstatFinalUtil;
import com.bjsxt.sm.common.util.PageInfoUtil;
import com.bjsxt.sm.users.pojo.Ausers;
import com.bjsxt.sm.users.service.IAcarsService;
import com.bjsxt.sm.users.service.IAchecktableService;
import com.bjsxt.sm.users.service.IUsersService;

@Controller
@RequestMapping("/usermanage/")
// 用户管理 管理员的controller
public class AdminsHeadController extends BaseController
{
	@Resource
	private IUsersService usersService;
	@Resource
	private IAcarsService carsService;


	/**
	 * 添加用户
	 * 
	 * @return
	 */
	@RequestMapping("/addUser")
	public String addUser()
	{
		ConstatFinalUtil.LOGGER.info("添加用户");
		return "/usermanage/addUser";
	}
	
	/**
	 * 提交添加用户
	 * 
	 * @return
	 */
	@RequestMapping("/addUserSubmit")
	public String addUserSubmit(Model model,Ausers ausers)
	{
		ConstatFinalUtil.LOGGER.info("提交添加用户");
		//给身份证号和邮箱密码加密    （ 到数据库里加密）
		ausers.setPassword(this.encryptUtil.encodeStr("password"));
		ausers.setIdentity(this.encryptUtil.encodeStr("identity"));
		//为在jsp上面没有属性赋值的后台赋值
		ausers.setCreateTime(new Date());
		ausers.setUpdateTime(new Date());
		JSONObject resultJSON = this.usersService .saveOneAusersService(ausers);
		String info = resultJSON.get("info")+"";
		/* 存储的结果给放到Request中 */
		model.addAttribute("info",info);
		return "/usermanage/addUser";
	}
	
	
	//查询用户   提交页面
	@RequestMapping("searchUser")
	public String searchUser(String email,String address, String status,String name,String identity,String currentPage,String pageSize,Model model)
	{
		/*
		 * 如果不传,默认可是null
		 * */
		if(email==null)
		{
			email="";
		}
		if(address==null)
		{
			address="";
		}
		if(status==null)
		{
			status="";
		}
		if(name==null)
		{
			name="";
		}
		if(identity==null)
		{
			identity="";
		}
		/* 条件 应该问JSP要,从Request中获取*/
		Map<String, Object> condMap = new HashMap<String,Object>();
		/* 按照关键字查询
		 * 模糊查询的问题 
		 * */
		if(!"".equalsIgnoreCase(email))
		{
			condMap.put("email", email);
		}
		if(!"".equalsIgnoreCase(address))
		{
			condMap.put("address", address);
		}
		if(!"".equalsIgnoreCase(status))
		{
			condMap.put("status", status);
		}
		if(!"".equalsIgnoreCase(name))
		{
			condMap.put("name", name);
		}
		if(!"".equalsIgnoreCase(identity))
		{
			condMap.put("identity", identity);
		}
		//分页
		PageInfoUtil pageInfoUtil = proccedPageInfoUtil(currentPage, pageSize);
		List<Ausers> users = this.usersService.findListAusersService(pageInfoUtil, condMap);
		//查出的数据放入四大作用于中
		model.addAttribute("pageInfoUtil",pageInfoUtil);
		model.addAttribute("users",users);
		model.addAttribute("email",email);
		model.addAttribute("address",address);
		model.addAttribute("status",status);
		model.addAttribute("name",name);
		model.addAttribute("identity",identity);
		ConstatFinalUtil.LOGGER.info("提交查询页面");
		return "/usermanage/searchUser";
	}
}
